package com.example.job.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.example.job.R;

/**
 * @author Deng
 * @describe 悬浮窗服务
 * @date 2023/5/14
 */
public class FloatingServer extends Service {
    public static boolean isStarted = false;

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    private View myView;
    private ImageView imageView;

    @Override
    public void onCreate() {
        super.onCreate();
        isStarted = true;
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 500;
        layoutParams.height = 100;
        layoutParams.x = 300;
        layoutParams.y = 300;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //showFloatingWindow();
        showFloatingWindowImage();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void showFloatingClickedWindow() {
        myView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.floating_clicked, null);
        windowManager.addView(myView, layoutParams);
        myView.setOnTouchListener(new FloatingOnTouchListener());

        ViewGroup.LayoutParams layoutParams = myView.getLayoutParams();
        layoutParams.height = 100;
        layoutParams.width = 100;
        windowManager.updateViewLayout(myView, layoutParams);
    }

    /**
     * image悬浮窗
     */
    @SuppressLint("ClickableViewAccessibility")
    private void showFloatingWindowImage() {
        imageView = new ImageView(getApplicationContext());
        imageView.setBackgroundResource(R.drawable.float_icon);
        windowManager.addView(imageView, this.layoutParams);
        imageView.setOnTouchListener(new FloatingOnTouchListener());

        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        layoutParams.height = 100;
        layoutParams.width = 100;
        windowManager.updateViewLayout(imageView, layoutParams);
    }

    /**
     * 操作事件
     */
    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;
        private int lastAction;

        @Override
        @SuppressLint("ClickableViewAccessibility")
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    lastAction = MotionEvent.ACTION_DOWN;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;
                    windowManager.updateViewLayout(view, layoutParams);
                    lastAction = MotionEvent.ACTION_MOVE;
                    break;
                case MotionEvent.ACTION_UP:
                    //单次点击
                    if(lastAction == MotionEvent.ACTION_DOWN){
                        showFloatingClickedWindow();
                        Log.i("NB", "FloatingService-单击");
                        windowManager.removeView(imageView);
                    }
                    break;
                default:
                    break;
            }
            return false;
        }
    }
}